package com.changcheng.controller;
import com.changcheng.mapper.PictureMapper;
import com.changcheng.pojo.Picture;
import com.changcheng.service.FileService;
import com.changcheng.service.PictureService;
import com.changcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@Api(tags = "产品图片操作")
@RequestMapping("/picture")
public class PictureController {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private FileService fileService;
    @Autowired
    private PictureMapper pictureMapper;
    //添加产品
    @PutMapping("/addProduct")
    @ApiOperation(value = "添加产品")
    public boolean addProduct(Picture picture, MultipartFile file, Model model) {
        //获取上传文件
        if (file.getSize() > 1024 * 1024 * 10) {
            model.addAttribute("msg", "文件太大");
        } else {
            String url = fileService.upload(file);
            picture.setPicture(url);
            pictureService.add(picture);
        }
        return true;
    }
    //删除
    @ApiOperation(value = "删除 (根据id删除)")
    @GetMapping("/delete")
    public R delete(Integer id, Model model) {
        pictureService.delete(id);
        return R.ok();
    }
    //修改
    @ApiOperation(value = "修改 (根据id修改)")
    @PostMapping("/update")
    public R update(Picture picture, Model model, MultipartFile file) {
        //获取上传文件
        if (file.getSize() > 1024 * 1024 * 10) {
            model.addAttribute("msg", "文件太大");
        } else {
            String url = fileService.upload(file);
            picture.setPicture(url);
            picture.setUpdate_time(LocalDate.now());
            pictureService.update(picture);
        }
        return R.ok();
    }
    @ApiOperation(value = "查询列表")
    @ResponseBody
    @GetMapping("/select")
    public List<Picture> select(Integer pageNum, Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        List<Picture> select = pictureService.select(pageNum,pageSize);
        return select;

    }
    @ApiOperation(value = "根据id查询")
    @ResponseBody
    @PostMapping("/findIdPicture")
    public List<Picture> findIdPicture(Integer id){
        List<Picture> findIdPicture = pictureService.findIdPicture(id);
        return findIdPicture;
    }
}
